package com.cstz.hb.manage.utils;

import com.cstz.enums.QuestionStatusEnum;
import com.cstz.hb.manage.domain.SysQuestion;
import com.cstz.ydshop.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.OutputStream;
import java.util.List;

/**
 * @author bo.deng
 * @Project: xy_article
 * @Package com.cstz.hb.manage.utils
 * @Description:
 * @date 2020/10/26  13:55
 */
@Slf4j
public class ExportExcelUtil {

    /**
     * 导出文件
     * @param list
     * @param outputStream
     */
    public static void exportExcelData(List<SysQuestion> list, OutputStream outputStream) throws Exception{
        HSSFWorkbook wb =  new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("问题列表");
        sheet.setDefaultRowHeightInPoints(30f);
        sheet.setColumnWidth(0,20*256);
        sheet.setColumnWidth(1,40*256);
        sheet.setColumnWidth(2,40*256);
        sheet.setColumnWidth(3,20*256);
        sheet.setColumnWidth(4,20*256);
        sheet.setColumnWidth(5,20*256);
        sheet.setColumnWidth(6,30*256);
        sheet.setColumnWidth(7,10*256);
        //标题
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)16);
        style.setFont(font);

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        sheet.addMergedRegion(new CellRangeAddress(0,1,0,7));
        cell.setCellValue("问题列表");
        cell.setCellStyle(style);

        row = sheet.createRow(2);

        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)14);
        style.setFont(font);
        style.setWrapText(true);

        cell = row.createCell(0);
        cell.setCellValue("问题类型");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("问题内容");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("解决措施");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("用户名称");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("用户所属单位名称");
        cell.setCellStyle(style);

        cell = row.createCell(5);
        cell.setCellValue("协助单位");
        cell.setCellStyle(style);

        cell = row.createCell(6);
        cell.setCellValue("创建时间");
        cell.setCellStyle(style);

        cell = row.createCell(7);
        cell.setCellValue("状态");
        cell.setCellStyle(style);

        int rowNums = 3;

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont cellFont = wb.createFont();
        cellFont.setFontHeightInPoints((short)12);
        cellStyle.setFont(cellFont);
        cellStyle.setWrapText(true);

        HSSFCellStyle dateStyle = wb.createCellStyle();
        dateStyle.setAlignment(HorizontalAlignment.CENTER);
        dateStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont dateCellFont = wb.createFont();
        dateCellFont.setFontHeightInPoints((short)12);
        dateStyle.setFont(dateCellFont);
        dateStyle.setDataFormat(wb.createDataFormat().getFormat(DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));

        for (SysQuestion sysQuestion : list) {
            HSSFRow rowItem = sheet.createRow(rowNums++);
            HSSFCell cellItem = rowItem.createCell(0);
            cellItem.setCellValue(sysQuestion.getTypeName());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(1);
            cellItem.setCellValue(sysQuestion.getContent());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(2);
            cellItem.setCellValue(sysQuestion.getSolutions());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(3);
            cellItem.setCellValue(sysQuestion.getUserName());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(4);
            cellItem.setCellValue(sysQuestion.getUserDeptName());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(5);
            cellItem.setCellValue(sysQuestion.getDeptName());
            cellItem.setCellStyle(cellStyle);
            cellItem = rowItem.createCell(6);
            cellItem.setCellValue(sysQuestion.getCreateTime());
            cellItem.setCellStyle(dateStyle);
            cellItem = rowItem.createCell(7);
            cellItem.setCellValue(QuestionStatusEnum.getName(sysQuestion.getStatus()));
            cellItem.setCellStyle(cellStyle);
        }
        wb.write(outputStream);
        wb.close();
    }

}
